<?php
class ControllerAccountEdit extends Controller {
	var $error = array();

	function index() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
	
		if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('account_edit'));

			$response->redirect($url->ssl('account_login'));
		}

		$language->load('controller/account_edit.php');

		$template->set('title', $language->get('heading_title'));

		if ($request->isPost() && $request->has('firstname', 'post') && $this->validate()) {
			$sql = "update customer set firstname = '?', lastname = '?', email = '?', telephone = '?', fax = '?', status = '1' where customer_id = '?'";
			$database->query($database->parse($sql, $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('email', 'post'), $request->gethtml('telephone', 'post'), $request->gethtml('fax', 'post'), $customer->getId()));

			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('account'));
		}

		$view = $this->locator->create('template');

		$view->set('tpl', $template);

		$view->set('heading_title', $language->get('heading_title'));

		$view->set('text_your_details', $language->get('text_your_details'));
		$view->set('text_your_password', $language->get('text_your_password'));

		$view->set('entry_firstname', $language->get('entry_firstname'));
		$view->set('entry_lastname', $language->get('entry_lastname'));
		$view->set('entry_date_of_birth', $language->get('entry_date_of_birth'));
		$view->set('entry_email', $language->get('entry_email'));
		$view->set('entry_telephone', $language->get('entry_telephone'));
		$view->set('entry_fax', $language->get('entry_fax'));
		$view->set('entry_password', $language->get('entry_password'));
		$view->set('entry_confirm', $language->get('entry_confirm'));

		$view->set('button_continue', $language->get('button_continue'));
		$view->set('button_back', $language->get('button_back'));

		$view->set('error', @$this->error['message']);
		$view->set('error_firstname', @$this->error['firstname']);
		$view->set('error_lastname', @$this->error['lastname']);
		$view->set('error_email', @$this->error['email']);
		$view->set('error_telephone', @$this->error['telephone']);
		$view->set('error_password', @$this->error['password']);
		$view->set('error_confirm', @$this->error['confirm']);

		$view->set('action', $url->ssl('account_edit'));

		if (($customer->getId()) && (!$request->isPost())) {
			$customer_info = $database->getRow("select * from customer where customer_id = '" . (int)$customer->getId() . "'");
		}

		if ($request->has('firstname', 'post')) {
			$view->set('firstname', $request->gethtml('firstname', 'post'));
		} else {
			$view->set('firstname', @$customer_info['firstname']);
		}

		if ($request->has('lastname', 'post')) {
			$view->set('lastname', $request->gethtml('lastname', 'post'));
		} else {
			$view->set('lastname', @$customer_info['lastname']);
		}

		if ($request->has('email', 'post')) {
			$view->set('email', $request->gethtml('email', 'post'));
		} else {
			$view->set('email',@$customer_info['email']);
		}

		if ($request->has('telephone', 'post')) {
			$view->set('telephone', $request->gethtml('telephone', 'post'));
		} else {
			$view->set('telephone',@$customer_info['telephone']);
		}

		if ($request->has('fax', 'post')) {
			$view->set('fax', $request->gethtml('fax', 'post'));
		} else {
			$view->set('fax',@$customer_info['fax']);
		}

		$view->set('password', $request->gethtml('password', 'post'));

		$view->set('confirm', $request->gethtml('confirm', 'post'));

		$view->set('back', $url->ssl('account'));

		$template->set('content', $view->fetch('content/account_edit.tpl'));

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function validate() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');		
		$request  =& $this->locator->get('request');
		$validate =& $this->locator->get('validate');
			
		if ((strlen($request->gethtml('firstname', 'post')) < 3) || (strlen($request->gethtml('firstname', 'post')) > 32)) {
			$this->error['firstname'] = $language->get('error_firstname');
		}

		if ((strlen($request->gethtml('lastname', 'post')) < 3) || (strlen($request->gethtml('lastname', 'post')) > 32)) {
			$this->error['lastname'] = $language->get('error_lastname');
		}
		if ((!$validate->strlen($request->gethtml('email', 'post'), 1, 32)) || (!$validate->email($request->gethtml('email', 'post')))) {
			$this->error['email'] = $language->get('error_email');
		}

		if ($database->getRow($database->parse("select * from customer where email = '?' and customer_id != '?'", $request->gethtml('email', 'post'), $customer->getId()))) {
			$this->error['message'] = $language->get('error_exists');
		}

		if ((strlen($request->gethtml('telephone', 'post')) < 3) || (strlen($request->gethtml('telephone', 'post')) > 32)) {
			$this->error['telephone'] = $language->get('error_telephone');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>
